﻿using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace GeneralTheGame
{
    public class ResultTable
    {
        public static void PrintTable(string[,] table, int countOfPlayer, int currentPlayer, int cursorPosLeft = 65, int cursorPosTop = 4)
        {
            int separator = 0;
            Console.ForegroundColor = ConsoleColor.White;
            for (int rows = 0; rows < table.GetLength(0); rows++)
            {
                Console.SetCursorPosition(cursorPosLeft, cursorPosTop + rows + separator);
                int rowLength = 0;
                separator++;
                for (int columns = 0; columns < countOfPlayer + 1; columns++)
                {
                    bool isBackgroundChanged = false;
                    if (columns == currentPlayer)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        isBackgroundChanged = true;
                    }

                    Console.Write(table[rows, columns]);
                    rowLength += table[rows, columns].Length;

                    if (isBackgroundChanged)
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                }
                Console.WriteLine("\n");
                Console.SetCursorPosition(cursorPosLeft, cursorPosTop + rows + separator);
                if (rows != table.GetLength(0) - 1)
                {
                   Console.WriteLine("{0}", new string('_', rowLength));
                }
            }
        }

        public static void SavePlayersMaxScore(string playerName, int playerScore)
        {
            string file = "MaxScore.txt";
            try 
	        {
                List<HighScore> winnersList = new List<HighScore>();
                foreach (string line in File.ReadAllLines(file))
                {
                    string[] row = line.Split(' ');
                    if (row.Length == 3)
                    {
                        winnersList.Add(new HighScore(row[1], int.Parse(row[2])));
                    }
                }
                winnersList.Add(new HighScore(playerName, playerScore));
                var newList = winnersList.OrderBy(w => w.Score).ToList();
                newList.Reverse();
                File.WriteAllLines(file, newList.ToArray().Select((x, y) => string.Format("{0}. {1} {2}", y + 1, x.Name, x.Score)));
	        }
	        catch (Exception)
	        {
                Console.WriteLine("Error while saving max players score to file");
	        }
        }

        public static string[,] CreateNewTable(int countOfPlayers)
        {
            string[,] resultTable = new string[18, countOfPlayers + 1];
            try
            {
                using (StreamReader sr = new StreamReader("ThrowingCombinations.txt", false))
                {
                    for (int row = 0; row < 18; row++)
                    {
                        string line = sr.ReadLine();
                        resultTable[row, 0] = (line + new string(' ', (17 - line.Length) / 2)).PadLeft(17);
                    }
                }
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(ex.Message);
            }
            catch (UnauthorizedAccessException ex)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(ex.Message);
            }
           
            return resultTable;
        }

        public static string[,] GetPlayerResults(string[,] results, Player[] players)
        {
            for (int player = 0; player < players.Length; player++)
            {
                for (int row = 0; row < results.GetLength(0); row++)
                {
                    if (row != 0 && row <results.GetLength(0) - 1)
                    {
                        if (players[player].PlayerScore[row - 1] == 0 && players[player].PlayerCombinations[row - 1])
                        {
                            results[row, player + 1] = (new string(' ', 5 / 2) + "_____").PadRight(10);
                        }
                        else
                        {
                            results[row, player + 1] = (new string(' ', (10 - players[player].PlayerScore[row - 1].ToString().Length) / 2)
                            + players[player].PlayerScore[row - 1].ToString()).PadRight(10);
                        }                 
                    }
                    else if (row == results.GetLength(0) - 1)
                    {
                        results[row, player + 1] = (new string(' ', (10 - players[player].TotalPlayerScore.ToString().Length) / 2)
                            + players[player].TotalPlayerScore).PadRight(10);
                    }
                    else
                    {
                        results[row, player + 1] = (new string(' ', (10 - players[player].PlayerName.Length) / 2)
                            + players[player].PlayerName).PadRight(10);
                    }
                }
            }

            return results;
        }
    }
}
